package org.dbstar.scheduler.control.db.dependency;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.apache.commons.lang.StringUtils;
import org.dbstar.scheduler.beans.JobDependency;
import org.dbstar.scheduler.beans.JobDependencyItem;
import org.dbstar.scheduler.control.db.MapJsonConfig;
import org.dbstar.scheduler.control.db.ObjectRowMapper;
import org.dbstar.scheduler.identifier.DefaultIdentifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.object.SqlQuery;


/**
 * JobDependency的Query对象
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-6-14
 */
public class JobDependencyQuery extends SqlQuery<JobDependency> {
	private static final JobDependencyRowMapper jobDependencyRowMapper = new JobDependencyRowMapper();

	public JobDependencyQuery(JdbcTemplate jdbcTemplate) {
		this(jdbcTemplate, null);
	}

	public JobDependencyQuery(JdbcTemplate jdbcTemplate, String condition) {
		super();
		setJdbcTemplate(jdbcTemplate);
		setSql("SELECT * FROM SCHD_JOB_DEPENDENCY" + (condition == null ? "" : (" " + condition)));
	}

	@Override
	protected final RowMapper<JobDependency> newRowMapper(Object[] parameters, @SuppressWarnings("rawtypes") Map context) {
		return jobDependencyRowMapper;
	}

	public static class JobDependencyRowMapper extends ObjectRowMapper<JobDependency> {
		@Override
		public JobDependency mapRow(ResultSet rs, int rowNum) throws SQLException {
			JobDependency dependency = new JobDependency();
			dependency.setIdentifier(DefaultIdentifier.valueOf(rs.getString("IDENTIFIER")));
			dependency.setJobConfIdentifier(DefaultIdentifier.valueOf(rs.getString("CONF_IDENTIFIER")));
			dependency.setName(rs.getString("NAME"));
			dependency.setDescription(rs.getString("DESCRIPTION"));
			dependency.setKeys(readKeys(rs.getString("KEY_DATA")));
			dependency.setParameters(readParameters(rs.getString("PARAMETER_DATA")));
			dependency.setDependencies(readDependencies(rs.getString("DEPENDENCY_DATA")));
			dependency.setCreateTime(rs.getLong("CREATE_TIME"));
			dependency.setOwner(rs.getString("OWNER"));
			return dependency;
		}

		private List<String> readKeys(String str) throws SQLException {
			if (str == null) {
				return null;
			} else {
				return Arrays.asList(StringUtils.split(str, ','));
			}
		}

		@SuppressWarnings("unchecked")
		private Map<String, String> readParameters(String str) throws SQLException {
			if (str == null) {
				return null;
			} else {
				JSONObject obj = JSONObject.fromObject(str);
				return (Map<String, String>) JSONSerializer.toJava(obj, new MapJsonConfig());
			}
		}

		@SuppressWarnings("unchecked")
		private Set<JobDependencyItem> readDependencies(String str) throws SQLException {
			if (str == null) {
				return null;
			} else {
				JSONArray array = JSONArray.fromObject(str);
				return (Set<JobDependencyItem>) JSONSerializer.toJava(array, new JobDependencyItemJsonConfig());
			}
		}
	}
}
